package com.bjsxt.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.Notice;
import com.bjsxt.model.Result;
import com.bjsxt.service.NoticeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;

/**
 * description:
 * 公告管理的控制层
 * 公告管理的权限:
 * 查询： notice_query
 * 修改： notice_update
 * 新增： notice_create
 * 删除： notice_delete
 *
 * @author ALIENWARE <br />
 * @date 2021/7/16
 */
@RestController
@RequestMapping("/notices")
@Api(tags = "公告管理的控制器")
public class NoticeController {

    /**
     * 公告管理业务层接口
     */
    @Autowired
    private NoticeService noticeService;


    /**
     * 分页查询公告数据
     *
     * @param page      分页对象<Notice>
     * @param title     标题
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param status    状态
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('notice_query')")
    @ApiOperation(value = "分页查询公告数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType = "query", example = "标题"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query", example = "1999-10-12"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query", example = "1999-10-13"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query", example = "1")
    })
    public Result<Page<Notice>> findByPage(
            @ApiIgnore Page<Notice> page,
            String title,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            Integer status
    ) {
        // 查询时间, 我们将最近新增的, 修改的数据优先展示, 排序 last_update_time
        page.addOrder(OrderItem.desc(Notice.COL_LAST_UPDATE_TIME));
        // 调用业务层进行分页数据的查询
        Page<Notice> noticePage = noticeService.pageByTitleAndCreateTimeAndStatus(page, title, startTime, endTime, status);
        // 返回响应数据
        return Result.success(noticePage);
    }

    /**
     * 分页查询公告数据
     *
     * @param page 分页对象<Notice>
     * @return 统一响应对象
     */
    @GetMapping("/simple")
    @ApiOperation(value = "分页查询公告数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20")
    })
    public Result<Page<Notice>> findByPage(@ApiIgnore Page<Notice> page) {
        // 调用业务层进行分页数据的查询
        Page<Notice> noticePage = noticeService.pageByStatus(page, 1);
        // 返回响应数据
        return Result.success(noticePage);
    }

    /**
     * 查询公告详情数据
     *
     * @param id 公告 ID
     * @return 统一响应对象
     */
    @GetMapping("/simple/{id}")
    @ApiOperation(value = "查询公告详情数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "Id", dataType = "Long", paramType = "query", example = "1")
    })
    public Result<Notice> getById(@PathVariable @NotNull Long id) {
        // 调用业务层进行数据的查询
        Notice notice = noticeService.getById(id);
        // 返回响应数据
        return Result.success(notice);
    }

    /**
     * 新增公告数据
     *
     * @param notice 公告对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('notice_create')")
    @ApiOperation(value = "新增公告数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "Notice 的 json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> add(@RequestBody @Validated Notice notice) {
        // 设置文章状态为 1
        notice.setStatus(1);
        // 调用业务层方法进行数据的保存
        boolean flag = noticeService.save(notice);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 更新公告数据
     *
     * @param notice 公告对象
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiOperation(value = "更新公告数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "Notice 的 json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> update(@RequestBody @Validated Notice notice) {
        // 调用业务层方法进行数据的保存
        boolean flag = noticeService.updateById(notice);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 启用/禁用一个公告
     *
     * @param id     要启用/禁用一个公告的公告 Id
     * @param status 要设置的公告状态值 1: 启用 0: 禁用
     * @return 统一响应对象
     */
    @PostMapping("/updateStatus")
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiOperation(value = "启用/禁用一个公告", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要启用/禁用一个公告的公告 Id", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "status", value = "要设置的公告状态值 1: 启用 0: 禁用", dataType = "Integer", paramType = "query", example = "0")
    })
    public Result<Void> updateStatus(@NotNull Long id, Integer status) {
        // 创建更新的 notice 对象
        Notice notice = new Notice();
        // 设置 id 及需要更新的值
        notice.setId(id);
        notice.setStatus(status);
        // 调用业务层方法进行数据的保存
        boolean flag = noticeService.updateById(notice);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 删除公告
     *
     * @param ids 删除公告 id 数组
     * @return 统一响应对象
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('notice_delete')")
    @ApiOperation(value = "删除公告", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "Notice 的 id 集合", dataType = "Long", required = true, paramType = "body", example = "1")
    })
    public Result<Void> delete(@RequestBody @NotEmpty Long[] ids) {
        // 调用业务层方法进行数据的更新
        boolean flag = noticeService.removeByIds(Arrays.asList(ids));
        // 如果为 true 删除成功
        if (flag) return Result.success("删除成功");
        // 如果为 false 删除失败
        return Result.fail("删除失败");
    }
}
